﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.Web.Controllers
{
    public class AddressController : Controller
    {
        // GET: Address
        /// <summary>
        /// 地址页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            string Name = User.Identity.Name;
            var NameId = CustomerService.GetCustomer(Name);
            int Id = NameId.Id;
            ViewBag.b = NameId.Id;
            List<Address> list = AddressService.GetAddresses(Id);
            return View(list);
        }
        /// <summary>
        /// 添加地址
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public ActionResult Add(Address address)
        {
            int a = AddressService.Addizhi(address);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "添加成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "添加失败" });
            }
        }

        [HttpGet]
        /// <summary>
        /// 修改页面
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult Update(int Id)
        {
            ViewBag.a = AddressService.GetAddressId(Id);
            string Name = User.Identity.Name;
            var NameId = CustomerService.GetCustomer(Name);
            ViewBag.b = NameId.Id;
            return View();
        }
        [HttpPost]
        public ActionResult Update(Address address ,int id)
        {
            int a = AddressService.UpdateDiZhi(address,id);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "修改成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "修改失败" });
            }
        }
        /// <summary>
        /// 设置默认地址
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetMR(int Id)
        {
            int a = AddressService.UpdateMoRen(Id);

            if (a > 0)
            {
                return Json(new { Success = true, Message = "设置成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "设置失败" });
            }
        }
        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int Id)
        {
            int a = AddressService.Delete(Id);

            if (a > 0)
            {
                return Json(new { Success = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "删除失败" });
            }
        }

    }
}